package jnpf.service.impl;



import jnpf.model.common.AnalyzeResult;
import jnpf.model.dify.talk.req.FlowTalkReq;
import jnpf.service.DifyFileService;
import jnpf.service.DifyTalkService;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service
@Slf4j
public class DifyFileServiceImpl implements DifyFileService {

    @Resource
    DifyTalkService difyTalkService;


    @Override
    public AnalyzeResult AnalyzeFile(String fileId) throws Exception {
        return null;
//        SysFile sysFile = sysFileDao.get(fileId);
//        if (sysFile == null) {
//            log.info("文件不存在");
//            return null;
//        } else {
//            String path = sysFile.getPath();
//            String os = System.getProperty("os.name").toLowerCase();
//            if (os.contains("win")) {
//                // 如果是Windows系统，获取项目文件的盘符并转换路径
//                File projectDir = new File(".");
//                String absolutePath = projectDir.getAbsolutePath();
//                String drive = absolutePath.split(":")[0];
//                // 假设路径以 / 开头，转换为 Windows 路径格式
//                if (path.startsWith("/")) {
//                    path = drive + ":" + path.replace("/", "\\");
//                }
//            } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
//
//            }
//            File file = new File(path);
//            String s = difyTalkService.uploadFile(file);
//            System.out.println(s);
//
//            //{"id": "680a7bac-c6a2-4259-8ace-631e8c993a5e", "name": "457093460605272065.ppt", "size": 2565120, "extension": "ppt", "mime_type": "application/octet-stream", "created_by": "2735fa56-33da-4b7d-b92c-ac80ea03cbe3", "created_at": 1743673025}
//            Gson gson = new Gson();
//            DifyUpdateResp difyUpdateResp = gson.fromJson(s, DifyUpdateResp.class);
//            //获取文件id
//            String difyFileId = difyUpdateResp.getId();
//            String type = "document";
//
//            DifyFileInfo fileInfo = new DifyFileInfo();
//            fileInfo.setUpload_file_id(difyFileId);
//            fileInfo.setType(type);
//            fileInfo.setTransfer_method("local_file");
//            ArrayList<DifyFileInfo> arrayList = new ArrayList<>();
//            arrayList.add(fileInfo);
//
//            WorkflowRunRequest workflowRunRequest = new WorkflowRunRequest();
//            workflowRunRequest.setUser( userProvider.get().getUserId());
//
//            WorkflowRunRequest.Input input = new WorkflowRunRequest.Input();
//            input.setFile(arrayList);
//            input.setType("DO名称列表解析");
//            workflowRunRequest.setInputs(input);
//
//            AnalyzeResult analyzeResult = difyTalkService.docTalkAi(workflowRunRequest, 8);
//            return analyzeResult;

    }

    @Override
    public AnalyzeResult AnalyzeFileDo( String doName, FlowTalkReq flowTalkReq) {
        FlowTalkReq.Input input = new FlowTalkReq.Input();
        input.setFile(flowTalkReq.getInputs().getFile());
        input.setType("DO详情解析");
        input.setDoName(doName);
        flowTalkReq.setInputs(input);
        AnalyzeResult analyzeResult = difyTalkService.docTalkAi(flowTalkReq, 8);
        analyzeResult.setDoName(doName);
        return analyzeResult;
    }


    @Override
    public ArrayList<AnalyzeResult> AnalyzeFileAllDo(ArrayList<String> doList, FlowTalkReq flowTalkReq) {
        ArrayList<AnalyzeResult> analyzeResults = new ArrayList<>();
        ArrayList<String> doNameList = new ArrayList<>();
        doNameList.add("电源管理系统");
        doNameList.add("蓝牙耳机芯片");
        doNameList.add("RT-Thread");
        for (String doName : doNameList) {
            FlowTalkReq.Input input = new FlowTalkReq.Input();
            input.setFile(flowTalkReq.getInputs().getFile());
            input.setType("DO详情解析");
            input.setDoName(doName);
            flowTalkReq.setInputs(input);
            AnalyzeResult analyzeResult = difyTalkService.docTalkAi(flowTalkReq, 8);
            analyzeResult.setDoName(doName);
            analyzeResults.add(analyzeResult);
        }
        return analyzeResults;
    }

}
